#include "terreno.h"
#include <stdio.h>
#include <algorithm>
#include <allegro.h>
#include "nelib.h"
#include <map>
using namespace std;


terreno::terreno(int x, int y, int w, int h, char *file) : elemento(x, y, w, h){
	char temp_bmp_name[150];
	char temp_att_name[150];
	char temp_basic_value[150];

	FILE *pterreno;/* = fopen("terreno/stats.nel", "r");
	
	while (!feof(pterreno)) {
		fscanf(pterreno,"%s ", temp_att_name);
		fscanf(pterreno,"%s ", temp_basic_value);
		if (!strcmp(temp_basic_value, "true"))
			this->stats[temp_att_name] = true;
		else
			this->stats[temp_att_name] = false;
	}
	fclose(pterreno);
*/
	pterreno = fopen(file, "r");
	if (pterreno != NULL) {
		fscanf(pterreno, "%s", temp_bmp_name);
		this->img = new imagem(this->x, this->y, temp_bmp_name);

		while (!feof(pterreno)) {
			fscanf(pterreno, "%s", temp_att_name);
			if (feof(pterreno))
				break;
			stats[temp_att_name] = true;
		}
	}
	else
		printf("Erro em carregar terreno %s", file);
	fclose(pterreno);
}

void terreno::draw(BITMAP *buffer, int fx, int fy, int fw, int fh) {
	int dx = 0, dy = 0;
	while(dy < h) {
		::draw(buffer, img->bmp, (x + dx), (y + dy), fx, fy, min(img->w, w - dx), min(img->h, h - dy), fw, fh);
		dx += min (img->w, w - dx);
		if (dx == w) {
			dx = 0;
			dy += min(img->h, h - dy);
		}
	}
}

